<template>
    <div>
        <el-dialog
            :title="$t('agent.agentAdd.title')"
            :visible="visibleAdd"
            class="el-dialog-lg"
            @close="()=>{$store.commit('setVisibleAdd',false)}">
            <el-form ref="form" :model="form" label-width="200px" >
                <el-row :gutter="24">
                    <el-col :span="11">
                        <el-form-item :label="$t('agent.agentAdd.firmType')">
                            <el-radio-group v-model="form.type">
                                <el-radio label="3">{{$t('agent')}}</el-radio>
                                <el-radio label="4">{{$t('installer')}}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item :label="$t('agent.firmName')" prop="firmName" :rules="[{ required: true, message: $t('index_js_notNull')},{validator:test_firmName,trigger:'blur'}]">
                            <el-input v-model="form.firmName" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('agent.firmShort')">
                            <el-input v-model="form.firmShort" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('login.loginName')" prop="userName" :rules="[{ required: true, message: $t('index_js_notNull')},{validator:test_loginName,trigger:'blur'}]">
                            <el-input v-model="form.userName" placeholder="" auto-complete="new-account"></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('password')" prop="password" :rules="[{ required: true, message: $t('index_js_notNull')},{validator:test_password,trigger:'blur'}]">
                            <el-input v-model="form.password" show-password placeholder="" auto-complete="new-password"></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('user.updatePassword.conPwd')" prop="repassword" :rules="[{ required: true, message: $t('index_js_notNull')},{validator:test_repassword,trigger:'blur'}]">
                            <el-input v-model="form.repassword" show-password placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item :label="$t('country')" prop="country" :rules="[{ required: true, message: $t('index_js_notNull')}]" >
                            <el-select v-model="form.country" filterable :placeholder="$t('pleaseSelect')">
                                <el-option
                                v-for="item in selectCountryData"
                                :key="item.enName"
                                :label="item.enName"
                                :value="item.enName">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item :label="$t('contact')">
                            <el-input v-model="form.relation" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('telphone')" prop="telphone" :rules="[{validator:test_telphone,trigger:'blur'}]">
                            <el-input v-model="form.telphone" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('email')" prop="email" :rules="[{ required: true, message: $t('index_js_notNull')},{validator:test_email,trigger:'blur'}]">
                            <el-input v-model="form.email" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('address')">
                            <el-input v-model="form.address" placeholder=""></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                
  
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setVisibleAdd',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')" :disabled="disabledBtn">{{$t('save')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import md5 from 'js-md5'
export default {
    props: {
        selectCountryData:{
            type:Array,
            default:() => {return []}
        },
    },
    data() {
        return {
            form:{
                type:'3',
                firmName:'',
                firmShort:'',
                userName:'',
                password:'',
                repassword:'',
                country:'',
                relation:'',
                telphone:'',
                email:'',
                address:''
            },
            test_firmName:{},
            test_loginName:{},
            test_password:{},
            test_repassword:{},
            test_telphone:{},
            test_email:{},
            disabledBtn:false
        };
    },
    computed: {
        visibleAdd(){
            return this.$store.state.sysStore.visibleAdd
        }
    },
    mounted(){
        this.test_firmName = async(rule, value, callback) => {
            let res = await this.API.agentFirmisUnique({
                firmName:this.form.firmName,
                type:this.form.type
            })
            if(!res.success){
                if(this.form.type==3){
                    callback(new Error(this.$t('agent.msg_unique_firmName')));
                }else{
                    callback(new Error(this.$t('install.msg_unique_installName')));
                }
            }else{
                callback()
            }
        }
        this.test_loginName = async(rule, value, callback) => {
            if(!this.Regs.loginName_reg.test(value)){
                callback(new Error(this.$t('login.js_sign_userName_error')));
            }else{
                let res = await this.API.agentUserisUnique({
                    loginName:this.form.userName
                })
                if(!res.success){
                    callback(new Error(this.$t('login.js_sign_userName_exist')));
                }else{
                    callback()
                }
            }
        }
        this.test_password = (rule, value, callback) => {
            if(this.Regs.space_reg.test(value)){
                callback(new Error(this.$t('pwd_contain_space')));
            }else{
                callback()
            }
        }
        this.test_repassword = (rule, value, callback) => {
            if(value!=this.form.password){
                callback(new Error(this.$t('login.js_sign_repwd_error')));
            }else{
                callback()
            }
        }
        this.test_telphone = (rule, value, callback) => {
            if(value!=''&&!this.Regs.phone_reg.test(value)){
                callback(new Error(this.$t('login.js_sign_phone_error')));
            }else{
                callback()
            }
        }
        this.test_email = (rule, value, callback) => {
            if(value!=''&&!this.Regs.email_reg.test(value)){
                callback(new Error(this.$t('login.js_sign_email_error')));
            }else{
                callback()
            }
        }
    },
    methods: {
        save(formName){
            this.disabledBtn=true
            this.$refs[formName].validate((valid) => {
              if (valid) {
                this.commitForm()
              } else {
                this.disabledBtn=false
                console.log('error submit!!');
                return false;
              }
            });
        },
        async commitForm(){
            this.form.password=md5(this.form.password)
            this.form.repassword=md5(this.form.repassword)
            let res = await this.API.agentAdd(Object.assign({},this.form,{enableFlag: 1}))
            if(res.success){
                this.disabledBtn=false
                this.$message.success(this.$t('index.control_success'));
            }else{
                this.disabledBtn=false
                this.$message.error(this.$t('index.control_fail'));
            }
            this.$store.commit('setVisibleAdd',false)
            this.$emit('uploadPage',true)
        }
    }
};
</script>

<style  lang="less">
</style>
